<?php

namespace Admin\Ims\Controllers;


use Phalcon\Mvc\Controller;
use Helpers\Main;

class ControllerBase extends Controller
{

    public function praseLog($detail,$msg)
    {
        return Main::createCommitLog(
            'ims',
            $this->dispatcher->getControllerName(),
            $this->dispatcher->getActionName(),
            ['post' => $this->request->getPost(), 'get' => $this->request->getQuery(), 'detail' => $detail],
            null,
            $msg
        );
    }

    public function beforeExecuteRoute($dispatcher)
    {
//        $userId=Main::getLoginUserInfo('userId');
//        if($this->dispatcher->getControllerName()=='moblie'&&$this->dispatcher->getActionName()!='login'){
//            if(!$userId){
//                return $this->response->redirect('ims/moblie/login');
//            }
//        }else{
//            if(!$userId){
//            return $this->dispatcher->forward([
//                'namespace'=>'Admin\Frontend\Controllers',
//                'controller'=> 'index',
//                'action'=>'login'
//            ]);
//            }
//        }
    }

    public function sendSuccessResponse($msg,$praseLog=true){
        if($praseLog){
            $this->praseLog($msg,$msg);
        }
        return $this->response->setContent($msg);
    }

    /**
     *
     * @param $code   400客户端 500服务器
     * @param $msg
     * @param bool $praseLog
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     */
    public function sendErrorResponse($code,$msg,$praseLog=true){
        if($praseLog){
            $this->praseLog($code.$msg,$msg);
        }
        return $this->response->setStatusCode($code)->setContent($msg);
    }

    public function sendJsonResponse($data,$praseLog=false,$msg=''){
        if($praseLog){
            $this->praseLog($msg,$msg);
        }
        return $this->response->setContent(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    
}